define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                let buttonName = row.money ;let ids = row.id ;
                                return `<a href="javascript:void(0);" class="btn btn-xs btn-primary btn-custom" data-id="`+ids+`" data-url="user/user/moneylog">${buttonName}</a>`;
                            }
                        },
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,

                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", null); // 隐藏编辑按钮
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('修改余额'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-credit-card',
                                    url: 'user/user/editmoney',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }, {
                                    name: 'detail',
                                    title: __('修改余额'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: 'user/user/edit',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }]
                        }
                    ]
                ]
            });
            Table.api.bindevent(table);
            $('body').on('click', '.btn-custom', function () {
                let rows = $(this).data('id'); // 获取当前行的数据
                console.log(rows);
                Fast.api.open('user/user/moneylog?ids='+rows, __('余额记录'), {row: rows});
            });

            // 为表格绑定事件

        },

        moneylog: function () {

                //
                // 初始化表格参数配置
                Table.api.init({
                    extend: {

                    }
                });

                var table = $("#table");

                var ids=$("#ids").val();

                // 初始化表格
                table.bootstrapTable({

                    url: 'user/user/moneylog?ids='+ids,
                    pk: 'id',
                    sortName: 'id',
                    search: false,
                    commonSearch: false,
                    searchFormVisible:false,
                    columns: [
                        [
                            {field: 'id', title: 'ID', operate: false},
                            {field: 'after', title: __('当前金额')},
                            {field: 'money', title: __('变更余额')},
                            {field: 'before', title: __('变更前余额')},
                            {field: 'memo', title: __('备注')},
                            {field: 'createtime', title: __('Create time')},

                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(table);
        },



        add: function () {
            Controller.api.bindevent();
        },

        editmoney: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});